/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;
using System.Xml;

namespace GrooveWebServicesV12Helpers.InfoPathForms
{
    /// <summary>
    /// Represents a Groove Sharepoint Files tool.
    /// </summary>
    public class InfoPathFormsTool : GrooveWebServicesV12Helpers.Forms2.FormsTool
    {
        public InfoPathFormsTool(GrooveWebServicesV12Helpers.Space i_Space, GrooveToolsWebService.Tool i_Tool)
            : base(i_Space, i_Tool, true)
        {
            //  Make sure we are dealing with an InfoPath Forms tool
            if (this.Type != GrooveWebServicesV12Helpers.Shared.GROOVE_INFOPATH_FORMS_TOOL_SUPPORTED_TYPE)
                throw new System.ArgumentException("Tool is not an InfoPath Forms tool!");
        }

        /// <summary>
        /// Array of Forms defined in the FormsTool.
        /// </summary>
        public new GrooveWebServicesV12Helpers.InfoPathForms.InfoPathForm[] Forms
        {
            get
            {
                System.Collections.ArrayList FormsArrayList = new System.Collections.ArrayList();

                //  Get initialized reference to web service
                GrooveForms2WebService.GrooveForms2 FormsWebService = base.GetInitializedFormsWebService(this.Data);

                System.DateTime BeginDateTime = System.DateTime.Now;

                //  Ask service to provide the list of Forms entries for this tool
                GrooveForms2WebService.Form[] Forms = FormsWebService.ReadForms();

                if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
                {
                    System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
                    System.Diagnostics.Debug.WriteLine("Forms2WebService::ReadForms, " + TimeSpan.TotalMilliseconds + " ms");
                }

                //  Create array list of Forms
                foreach (GrooveForms2WebService.Form Form in Forms)
                {
                    System.Diagnostics.Debug.Assert(Form.Type == "InfoPath");

                    GrooveWebServicesV12Helpers.InfoPathForms.InfoPathForm NewForm = new GrooveWebServicesV12Helpers.InfoPathForms.InfoPathForm
                        (
                        this,
                        Form
                        );

                    FormsArrayList.Add(NewForm);
                }

                //  Return array from array list
                return (GrooveWebServicesV12Helpers.InfoPathForms.InfoPathForm[])FormsArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.InfoPathForms.InfoPathForm));
            }
        }

        public const string MSO_IPSOLUTION = "mso-infoPathSolution";
        public const string HREF = "href";
        public const string SOLUTION_VERSION = "solutionVersion";
        public const string NAME = "name";

        public static void GetPIAttributes(XmlDocument i_XmlDoc, out string o_HRef, out string o_SolutionVersion, out string o_Name)
        {
            o_HRef = GetHRefAttribute(i_XmlDoc);
            o_SolutionVersion = GetSolutionVersionAttribute(i_XmlDoc);
            o_Name = GetNameAttribute(i_XmlDoc);
        }

        public static void SetPIAttributes(XmlDocument i_XmlDoc, string i_HRef, string i_SolutionVersion, string i_Name)
        {
            SetHRefAttribute(i_XmlDoc, i_HRef);
            SetSolutionVersionAttribute(i_XmlDoc, i_SolutionVersion);
            SetNameAttribute(i_XmlDoc, i_Name);
        }

        public static string GetHRefAttribute(XmlDocument i_XmlDoc)
        {
            return XMLHelper.GetProcessingInstructionAttributeValue(i_XmlDoc, MSO_IPSOLUTION, HREF);
        }

        public static void SetHRefAttribute(XmlDocument i_XmlDoc, string i_HRef)
        {
            XMLHelper.SetProcessingInstructionAttributeValue(i_XmlDoc, MSO_IPSOLUTION, HREF, i_HRef);
        }

        public static string GetSolutionVersionAttribute(XmlDocument i_XmlDoc)
        {
            return XMLHelper.GetProcessingInstructionAttributeValue(i_XmlDoc, MSO_IPSOLUTION, SOLUTION_VERSION);
        }

        public static void SetSolutionVersionAttribute(XmlDocument i_XmlDoc, string i_SolutionVersion)
        {
            XMLHelper.SetProcessingInstructionAttributeValue(i_XmlDoc, MSO_IPSOLUTION, SOLUTION_VERSION, i_SolutionVersion);
        }

        public static string GetNameAttribute(XmlDocument i_XmlDoc)
        {
            return XMLHelper.GetProcessingInstructionAttributeValue(i_XmlDoc, MSO_IPSOLUTION, NAME);
        }

        public static void SetNameAttribute(XmlDocument i_XmlDoc, string i_Name)
        {
            XMLHelper.SetProcessingInstructionAttributeValue(i_XmlDoc, MSO_IPSOLUTION, NAME, i_Name);
        }
    }
}
